import os, xbmc, xbmcgui, xmlrpc2scgi


MyPath  = os.getcwd()
MyMedia = MyPath +'\\media'

rpc = xmlrpc2scgi.RTorrentXMLRPCClient("scgi://localhost:5000")

#get actioncodes from keymap.xml
ACTION_PREVIOUS_MENU = 10
ACTION_SELECT_ITEM = 7

def fetchDetail(hash, method):
        obj = eval("rpc.d."+method)
        result = None
        if not method.startswith("__") and callable(obj):
            try:
                result = obj(hash)
            except:
                pass

        return result
    
class TorrentDetails(xbmcgui.Window):
    
    def __init__(self):
        print '--TorrentDetails Init-- '+str(self.getWidth())+' '+str(self.getHeight())
        self.bg = xbmcgui.ControlImage( 0, 0, self.getWidth(), self.getHeight(), 'background.png', aspectRatio=0 )
        self.addControl( self.bg )
        self.strActionInfo = xbmcgui.ControlLabel(20, 60, 500, 200, '', 'font13', '0xFFFF00FF')
        self.addControl(self.strActionInfo)
        self.textBox = xbmcgui.ControlTextBox(100, 120, 500, 200, 'font13', '0xFFFF00FF')
        self.addControl(self.textBox)
        self.detailInfo = xbmcgui.ControlList(100, 100, 600, 600, '', 'font13', '0xFFFF00FF')
        #self.detailInfo.setHeight(30)
        self.detailInfo.setImageDimensions(50, 50)
        self.addControl(self.detailInfo)
        # add pause / resume button
        self.pause = xbmcgui.ControlButton(100, 700, 100, 50, 'Pause' )
        self.addControl(self.pause)
        # stop button
        self.stop = xbmcgui.ControlButton(200, 700, 100, 50, 'Stop' )
        self.addControl(self.stop)
        # ok button
        self.ok = xbmcgui.ControlButton(300, 700, 100, 50, 'OK' )
        self.addControl(self.ok)


    def onControl(self, control):
        #self.strActionInfo.setLabel('control '+control.name())

        if control == self.ok:
            self.close()

        if control == self.pause:
            #call rpc
            if fetchDetail(hash, 'is_active') == 1:
                fetchDetail(hash, 'pause')
            else:
                fetchDetail(hash, 'resume')

        if control == self.delete:
            #call rpc
            dlg = xbmcdialog.Dialog()
            if dgl.yesno('Delete torrent', 'Are you sure you want to delete', fetchDetail(hash,'get_name')):
                fetchDetail(hash, 'erase')



    def message(self, message):
        print '--TorrentDetails message--'
        self.textBox.setText(message)

    def DETAILS(self, hash):
        print '--DETAILS--'+hash

        self.strActionInfo.setLabel('Torrent: '+fetchDetail(hash,'get_name'))
        Det = [['' for i in range(3)] for j in range(11)]
        #display all the details associated with the selected torrent

        Det[0][0] = 'Ratio'
        Det[0][1]= rpc.d.get_ratio(hash)
        Det[0][2] = 1
        Det[1][0] = 'Size'
        Det[1][1]= rpc.d.get_size_bytes(hash) / 1024
        Det[1][2] = 1
        Det[2][0] = 'Downloaded'
        Det[2][1] = rpc.d.get_bytes_done(hash) / 1024
        Det[2][2] = 1
        Det[3][0] = 'Uploaded'
        Det[3][1]= rpc.d.get_up_total(hash) / 1024
        Det[3][2] = 1
        Det[4][0] = 'Chunk Size'
        Det[4][1] = fetchDetail(hash, 'get_chunk_size')
        Det[4][2] = 1
        Det[5][2] = 0
        Det[6][0] = 'Complete'
        if fetchDetail(hash, 'get_complete') == 1:
            Det[6][1] = 'Completed'
        else:
            Det[6][1] = 'Incomplete'
        Det[6][2] = 1
        Det[7][0] = 'Completed Bytes'
        Det[7][1] = fetchDetail(hash, 'get_completed_bytes')
        Det[7][2] = 1
        Det[8][0] = 'Completed Chunks'
        Det[8][1] = fetchDetail(hash, 'get_completed_chunks')
        Det[8][2] = 1
        Det[9][0] = 'Current Connections'
        Det[9][1] = fetchDetail(hash, 'get_connection_current')
        Det[9][2] = 1
        Det[10][0] = 'Leech Connections'
        Det[10][1] = fetchDetail(hash, 'get_connection_leech')
        Det[10][2] = 1
#        get_connection_seed = fetchDetail(hash, 'get_connection_seed')
#        get_creation_date = fetchDetail(hash, 'get_creation_date')
#        get_down_rate = fetchDetail(hash, 'get_down_rate')
#        get_down_total = fetchDetail(hash, 'get_down_total')
#        get_left_bytes = fetchDetail(hash, 'get_left_bytes')
#        get_message = fetchDetail(hash, 'get_message')
#        get_mode = fetchDetail(hash, 'get_mode')
#        get_peer_exchange = fetchDetail(hash, 'get_peer_exchange')
#        get_peers_accounted = fetchDetail(hash, 'get_peers_accounted')
#        get_peers_complete = fetchDetail(hash, 'get_peers_complete')
#        get_peers_connected = fetchDetail(hash, 'get_peers_connected')
#        get_peers_not_connected = fetchDetail(hash, 'get_peers_not_connected')
#        get_ratio = fetchDetail(hash, 'get_ratio')
#        get_size_bytes = fetchDetail(hash, 'get_size_bytes')
#        get_size_chunks = fetchDetail(hash, 'get_size_chunks')
#        get_size_files = fetchDetail(hash, 'get_size_files')
#        get_tracker_focus = fetchDetail(hash, 'get_tracker_focus')
#        get_tracker_numwant = fetchDetail(hash, 'get_tracker_numwant')
#        get_tracker_size = fetchDetail(hash, 'get_tracker_size')
#        is_private = fetchDetail(hash, 'is_private')


        n = 0
        for _d in Det :
                if _d[2] == 1:
                    Image  =   MyMedia +'\\Health-2.png'
                    #detail = xbmcgui.ListItem(str(_d[0]), str(_d[1]), Image, Image)
                    self.detailInfo.addItem(xbmcgui.ListItem(str(_d[0]), str(_d[1]), Image, Image))
                    print '-- added --' + str(_d[0]) + ' ' + str(_d[1])
